<?php
/*
V.8 Rev.7
-$makan = input
-$minum = output
-countWhere(masukan, operator, dimana)
-inWhere(masukan, dimana)
-quickCount(masukan)
-sorting(masukan)
By: Mahadiputra S
svn http://code.google.com/p/mahadiputra-ta
*/
?>

<?php
/*==========================================================================================
Hitung banyaknya.....
==========================================================================================*/
function countWhere($makan = 1, $operator = '==', $where = 1)
 {
     $makan = is_array($makan) ? $makan : (array)$makan;
     $operator = !in_array($operator, array('<','>','<=', '>=','==', '!=')) ? '==' : $operator;
     $i = 0;
     foreach($makan as $current)
     {
         $match = null;
         eval('$match = (bool)("'.$current.'"'.$operator.'"'.$where.'");');
         $i = $match ? $i+1 : $i;
     }
     return $i;
	 unset($current);
 }
/*==========================================================================================
Cari dimana.....
==========================================================================================*/
function inWhere($makan = 1, $where = 1) //input =array, where =pembanding
{
$makan = is_array($makan) ? $makan : (array)$makan;
$i=1;$j=1;
	foreach($makan as $current)
	{
	if ($current==$where)
	{/*echo "[".$i."]";*/
	$j=$i;}
	$i++;
	}
return $j;
unset($current);
}
//echo "start<br/>";for ($a=1;$a<=30;$a++){$makan[$a]=mt_rand(1,35);echo $makan[$a]." ; ";}
//$b=count($makan);echo "<br/>".$b."<br/>";
/*==========================================================================================
Fitness slot + waktu + bentrok + waktu2 tanpa Tabel.....
==========================================================================================*/
function quickCount($makan)
{
$unik = array_unique($makan);	//dapetin angka unik
$hitung = count($unik);			//hitung ada berapa angka unik
foreach ($unik as &$value) {	//rubah angka unik; array() => array[]
    $unik2[]=$value;
	//echo $value.";";
}unset($value);
$jumlah = array_count_values($makan);	//hitung banyaknya tiap angka unik
//print_r($jumlah);
foreach ($jumlah as &$value) {	//rubah banyaknya angka unik; array() => array[]
    $jumlah2[]=$value;
	//echo $value.";";
}unset($value);
//print_r($unik);
//echo "<br/>".$hitung."<br/>";
for ($a=0;$a<$hitung;$a++)				//fitness 1
{
if ($jumlah2[$a]==1)
{$hh[$a]=4;}
elseif ($jumlah2[$a]==2)
{$hh[$a]=1;}
else {$hh[$a]=10;}
}
$fit_slot_tot=array_sum($hh);
for ($a=0;$a<$hitung;$a++){				//fitness 2
if ($unik2[$a]<=3){ $fit_efek[$a]=($jumlah2[$a])*2;}
elseif ($unik2[$a]<=6){ $fit_efek[$a]=($jumlah2[$a])*4;}
elseif ($unik2[$a]<=9){ $fit_efek[$a]=($jumlah2[$a])*6;}
elseif ($unik2[$a]<=12){ $fit_efek[$a]=($jumlah2[$a])*8;}
elseif ($unik2[$a]<=15){ $fit_efek[$a]=($jumlah2[$a])*10;}
elseif ($unik2[$a]<=18){ $fit_efek[$a]=($jumlah2[$a])*12;}
elseif ($unik2[$a]<=21){ $fit_efek[$a]=($jumlah2[$a])*14;}
elseif ($unik2[$a]<=24){ $fit_efek[$a]=($jumlah2[$a])*16;}
elseif ($unik2[$a]<=27){ $fit_efek[$a]=($jumlah2[$a])*18;}
elseif ($unik2[$a]<=30){ $fit_efek[$a]=($jumlah2[$a])*20;}
}
$fit_efek_tot=array_sum($fit_efek);
for($bnt=0;$bnt<$hitung;$bnt++){					//urutan buat fitness 3
$jml_bnt=$jumlah2[$bnt];
$unik_bnt=$unik2[$bnt];

if ($jml_bnt<=1){}
elseif ($jml_bnt<=2)
				{
				//echo $unik_bnt." :";
				//echo "ada 2 ";
				$knci=array_keys($makan, $unik_bnt);
				foreach ($knci as &$value) {
					$knci2[]=$value;
					}
				$pintu1=$knci2[0];$pintu2=$knci2[1];
				//echo $pintu1;echo " : ".$pintu2." = ";
				$fit_bnt[$bnt]= $hasil2[$pintu1][$pintu2]*10;
				//if ($bndg_bnt0>=1){$fit_bnt[$bnt]="10";}
				//else {echo " g bentrok<br />";}
				//echo $fit_bnt[$bnt]."<br />";
				unset($value);unset($knci);unset($knci2);
				}
elseif ($jml_bnt<=3)
				{
				//echo $unik_bnt." :";
				//echo "ada 3 ";
				$knci=array_keys($makan, $unik_bnt);
				foreach ($knci as &$value) {
					$knci2[]=$value;
					}
				$jndela1=$knci2[0];$jndela2=$knci2[1];$jndela3=$knci2[2];
				//echo $jndela1;echo " : ".$jndela2;echo " : ".$jndela3." = ";
				$fit_bnt1 = $hasil2[$jndela1][$jndela2] * 10;
				$fit_bnt2 = $hasil2[$jndela1][$jndela3] * 10;
				$fit_bnt3 = $hasil2[$jndela2][$jndela3] * 10;
				$fit_bnt[$bnt] = $fit_bnt1+$fit_bnt2+$fit_bnt3."<br />";
				//echo $fit_bnt[$bnt]
				unset($value);unset($knci);unset($knci2);
				}
else {$fit_bnt[$bnt]="40";}
}

$fit_bnt_tot=array_sum($fit_bnt);

$fit_total=round(1/($fit_slot_tot+$fit_efek_tot+$fit_bnt_tot), 6);	//total fitness
return $fit_total;

/*echo "<table border='1'>";	//Cara 1
echo "<tr><td></td><td>count</td><td>fitnes-slot</td><td>fitnes-efek-slot</td></tr>";
for ($a=0;$a<$hitung;$a++){
echo "<tr><td>".$unik2[$a]."</td>";	//angka
echo "<td>".$jumlah2[$a]."</td>";	//count
echo "<td>".$hh[$a]."</td>";	//fitnes
echo "<td>".$fit_efek[$a]."</td></tr>";}
echo "<tr><td>".$fit_slot_tot."</td><td>".$fit_efek_tot."</td><td>".$fit_total."</td></tr>";
echo "</table>";*/
/*echo "\n<table border='1'>";	//cara 2, kl pk ini uncomment fungsi 1...
for ($a=0;$a<$hitung;$a++)
{
echo "<tr><td>". $unik2[$a]."</td>";
echo "<td>".countwhere($makan, '==', $unik2[$a])."</td></tr>";
}
echo "</table>";*/
}
/*==========================================================================================
Sorting fitness.....
==========================================================================================*/
function sorting($makan=1)
{arsort($makan);
foreach ($makan as $key => $val){
//echo "$key = $val";
$i++;$j[$i]=$key;
}
unset($key);unset($val);
return $j;
}
/*==========================================================================================
Mutasi cuy.....
==========================================================================================*/
function mutasiin ($camilan=1)
{
switch ($camilan){
case 1: $balik=16;break;
case 2: $balik=17;break;
case 3: $balik=18;break;
case 4: $balik=19;break;
case 5: $balik=20;break;
case 6: $balik=21;break;
case 7: $balik=22;break;
case 8: $balik=23;break;
case 9: $balik=24;break;
case 10: $balik=25;break;
case 11: $balik=26;break;
case 12: $balik=27;break;
case 13: $balik=28;break;
case 14: $balik=29;break;
case 15: $balik=30;break;
case 16: $balik=1;break;
case 17: $balik=2;break;
case 18: $balik=3;break;
case 19: $balik=4;break;
case 20: $balik=5;break;
case 21: $balik=6;break;
case 22: $balik=7;break;
case 23: $balik=8;break;
case 24: $balik=9;break;
case 25: $balik=10;break;
case 26: $balik=11;break;
case 27: $balik=12;break;
case 28: $balik=13;break;
case 29: $balik=14;break;
case 30: $balik=15;break;
default: $balik=1;
}
return $balik;
}
?>

<?php/*
$ver= 8;
$rev= 7;
$author= "Mahadiputra S";
$path= "http://code.google.com/p/mahadiputra-ta"
*/
?>
